home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: MegaDisc / MegaDisc 27 (1992-03)(MegaDisc Digital Publishing)(AU)(Disk 2 of 2).zip / MegaDisc 27 (1992-03)(MegaDisc Digital Publishing)(AU)(Disk 2 of 2).adf / Programming / Random / randtest.c < prev    next >
C/C++ Source or Header  |  1992-03-30  |  1KB  |  54 lines

  1.  /*
  2.  Code is in the public domain.
  3.  Can you seriously envisage ANYONE paying to use this mess? I can't.
  4.     - Peter Thompson 14 Feb 92
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include "random.h"
  9.  
  10. main(int argc, char *argv[])
  11. {
  12.   RandState *randbase;
  13.   long i;
  14.   long j;
  15.   long *counts;
  16.   long N;
  17.   long R;
  18.   ULONG seed;
  19.   double sum = 0;
  20.   double chisqr = 0;
  21.  
  22.   seed = (ULONG) atol(argv[1]);
  23.   N = atol(argv[2]);
  24.   R = atol(argv[3]);
  25.   printf("Seed:%li N:%li R:%li\n", seed, N, R);
  26.   counts = (long *) calloc(R,sizeof(long int));
  27.   if ((long *) NULL == counts)
  28.     exit(21);
  29.   randbase = (RandState *) malloc(sizeof(RandState));
  30.   if ((RandState *) NULL == randbase)
  31.     exit(21);
  32.   rand32init(randbase, seed);
  33.   for (i = N; (i>0); i--) {
  34.     ULONG result;
  35.     result = rnda32(randbase);
  36.  /* Debug stuff
  37.     for (j = 0; j < 64; j++)
  38.       printf("%8.8x%c",(ULONG)randbase->rs_State[j],(((j+1)&7)==0)? '\n':' ');
  39.     printf("%li %x\n",randbase->rs_Index,2*result);    
  40.  */
  41.     counts[(long)(R*(result/(float) MAXRNDNUM))]++;
  42.   }
  43.   for (i = 0, sum = 0; i < R; i++) {
  44.     sum += counts[i]*counts[i];
  45.     if (i < 128)
  46.       printf("%5.5li%c",counts[i],(((i+1)&7)==0)? '\n':' ');
  47.   }
  48.   chisqr = R*(sum/(double)N)-N;
  49.   if (((chisqr-R)*(chisqr-R)) > (4*R))
  50.     printf("\nFailed: %f\n",chisqr);
  51.   else
  52.     printf("\nPassed: %f\n",chisqr);
  53. }
  54.